/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 
 * File:   genericGenerator.h
 * Author: picci
 *
 * Created on November 13, 2010, 5:18 PM
 */

#ifndef GENERICGENERATOR_H
#define	GENERICGENERATOR_H

#include "prj_definitions.h"
#include "generators/generators.h"

namespace NAMESPACE{

template<typename T, typename Base = T>
class GenericGenerator : public AbstractGenerator{
public:
    typedef T generated_t;

    using AbstractGenerator::generate;
    using AbstractGenerator::set_key;
private:
    memory_size_t  mem_emp;

    virtual void free_element(IOElement* el){
        T* generated = boost::any_cast<T*>( el->getElement() );
        delete generated;
    }

    virtual memory_size_t measure_memory(){
        return generated_buffer->size() * 1000000;
    }

public:
    ~GenericGenerator(){ free_buffer(); }
    GenericGenerator() : AbstractGenerator(),
                         mem_emp(0){
        key->setType(TypeParseTraits<Base*>::name());
    }
    GenericGenerator(const GenericGenerator& gg) : AbstractGenerator(gg),
                                                   mem_emp(gg.mem_emp){
        key->setType(TypeParseTraits<Base*>::name());
    }
    GenericGenerator& operator=(const GenericGenerator& gg){
        if(this == &gg) return *this;
        AbstractGenerator::operator =(gg);
        mem_emp = gg.mem_emp;
        return *this;
    }

    GenericGenerator(const IOKey& k, 
                     const memory_size_t& _max_mem = AbstractGenerator::DEFAULT_MAX_MEM_SIZE)
                     : AbstractGenerator(k, _max_mem),
                       mem_emp(0){
        key->setType(TypeParseTraits<Base*>::name());
    }

    virtual Generator& set_key(const std::string& ns, const std::string& key_name){
        AbstractGenerator::set_key(ns, key_name);
        key->setType(TypeParseTraits<Base*>::name());
        return *this;
    }

    IOElement* generate(const IOKey& key){
        return new HandledIOElement(new generated_t(), key);
    }

    virtual bool operator==(const Generator& g) const{
        const GenericGenerator* gg = dynamic_cast<const GenericGenerator*>(&g);
        if(!gg) return false;

        if(AbstractGenerator::operator!=(g)) return false;

        return mem_emp == gg->mem_emp;
    }

    virtual bool operator!=(const Generator& g) const{ 
        return !GenericGenerator::operator ==(g);
    }
};

}

#endif	/* GENERICGENERATOR_H */

